// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <gtest/gtest.h>

#include <pollux/experimental/wave/common/Cuda.h>
#include <pollux/experimental/wave/common/Hash.h>

namespace kumo::pollux::wave {
namespace {

__global__ void murmur3(const char* data, size_t len, uint32_t* out) {
  *out = Murmur3::hashBytes(data, len, 42);
}

void testMurmur3(const std::string& s, uint32_t expected) {
  SCOPED_TRACE(s);
  auto* allocator = getAllocator(getDevice());
  auto buf = allocator->allocate<char>(s.size() + 1);
  memcpy(&buf[1], s.data(), s.size());
  auto actual = allocator->allocate<uint32_t>();
  murmur3<<<1, 1>>>(&buf[1], s.size(), actual.get());
  ASSERT_EQ(cudaGetLastError(), cudaSuccess);
  ASSERT_EQ(cudaDeviceSynchronize(), cudaSuccess);
  ASSERT_EQ(*actual, expected);
}

TEST(HashTest, murmur3) {
  testMurmur3("foo", 1015597510u);
  testMurmur3("foobar", 3446066726u);
}

} // namespace
} // namespace kumo::pollux::wave
